<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java synchronized 关键字深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            border-left: 4px solid #667eea;
        }
        .summary-card {
            transition: all 0.3s ease;
            border-left: 4px solid transparent;
        }
        .summary-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            border-left: 4px solid #667eea;
        }
        .feature-icon {
            color: #667eea;
            font-size: 1.5rem;
        }
        .scroll-indicator {
            animation: bounce 2s infinite;
        }
        @keyframes bounce {
            0%, 20%, 50%, 80%, 100% {transform: translateY(0);}
            40% {transform: translateY(-15px);}
            60% {transform: translateY(-7px);}
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Java <span class="text-yellow-200">synchronized</span> 关键字深度解析</h1>
                    <p class="text-xl mb-8 opacity-90">掌握多线程同步的核心机制，构建线程安全的Java应用</p>
                    <div class="flex space-x-4">
                        <a href="#types" class="bg-white text-purple-800 hover:bg-gray-100 px-6 py-3 rounded-lg font-medium transition duration-300">探索用法</a>
                        <a href="#summary" class="border-2 border-white text-white hover:bg-white hover:text-purple-800 px-6 py-3 rounded-lg font-medium transition duration-300">快速总结</a>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="w-full max-w-md bg-white bg-opacity-10 p-6 rounded-xl backdrop-blur-sm border border-white border-opacity-20">
                        <div class="mermaid">
                            graph TD
                                A[synchronized] --> B[实例方法锁]
                                A --> C[静态方法锁]
                                A --> D[同步代码块]
                                D --> E[任意对象锁]
                                D --> F[类对象锁]
                                D --> G[this锁]
                        </div>
                    </div>
                </div>
            </div>
            <div class="text-center mt-16 scroll-indicator">
                <i class="fas fa-chevron-down text-2xl opacity-70"></i>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-6xl px-4 py-16">
        <!-- Introduction -->
        <section class="mb-20">
            <div class="bg-gray-50 p-8 rounded-xl">
                <h2 class="text-3xl font-bold mb-6 text-gray-800">线程同步的守护者</h2>
                <p class="text-lg text-gray-700 mb-6"><span class="text-purple-600 font-medium">synchronized</span> 关键字是 Java 中实现线程同步的核心机制，它可以确保同一时刻只有一个线程能执行某个代码块或方法，有效解决多线程环境下的数据竞争问题。</p>
                <div class="flex flex-wrap -mx-2">
                    <div class="w-full md:w-1/2 px-2 mb-4">
                        <div class="bg-white p-6 rounded-lg shadow-sm h-full">
                            <div class="flex items-center mb-4">
                                <div class="bg-purple-100 p-3 rounded-full mr-4">
                                    <i class="fas fa-lock feature-icon"></i>
                                </div>
                                <h3 class="text-xl font-semibold">互斥访问</h3>
                            </div>
                            <p class="text-gray-600">确保共享资源在同一时间只被一个线程访问，防止数据不一致。</p>
                        </div>
                    </div>
                    <div class="w-full md:w-1/2 px-2 mb-4">
                        <div class="bg-white p-6 rounded-lg shadow-sm h-full">
                            <div class="flex items-center mb-4">
                                <div class="bg-purple-100 p-3 rounded-full mr-4">
                                    <i class="fas fa-exchange-alt feature-icon"></i>
                                </div>
                                <h3 class="text-xl font-semibold">内存可见性</h3>
                            </div>
                            <p class="text-gray-600">保证一个线程对共享变量的修改对其他线程立即可见。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Types of Synchronized -->
        <section id="types" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-center text-gray-800">五种同步方式解析</h2>
            
            <!-- 实例方法锁 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="bg-purple-600 text-white w-10 h-10 rounded-full flex items-center justify-center mr-4">
                        <span class="font-bold">1</span>
                    </div>
                    <h3 class="text-2xl font-semibold text-gray-800">实例方法锁（对象锁）</h3>
                </div>
                <div class="flex flex-col md:flex-row -mx-4">
                    <div class="md:w-1/2 px-4 mb-6 md:mb-0">
                        <div class="code-block text-gray-200 p-6 rounded-lg overflow-x-auto">
                            <pre><code>public class SynchronizedInstanceMethodDemo {
    public synchronized void instanceMethod() {
        // 临界区代码
        System.out.println("实例方法锁：线程 " + 
            Thread.currentThread().getName() + " 正在执行");
    }
}</code></pre>
                        </div>
                    </div>
                    <div class="md:w-1/2 px-4">
                        <div class="bg-gray-50 p-6 rounded-lg h-full">
                            <h4 class="text-lg font-semibold mb-4 text-gray-700">作用范围与适用场景</h4>
                            <p class="mb-4"><span class="font-medium text-purple-600">锁对象：</span>锁住的是调用该实例方法的对象实例。</p>
                            <p><span class="font-medium text-purple-600">适用场景：</span>当你希望在同一个对象的多个线程中保持对某个方法的互斥访问时使用。例如，确保一个对象的状态在方法执行期间不会被其他线程改变。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 静态方法锁 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="bg-purple-600 text-white w-10 h-10 rounded-full flex items-center justify-center mr-4">
                        <span class="font-bold">2</span>
                    </div>
                    <h3 class="text-2xl font-semibold text-gray-800">静态方法锁（类锁）</h3>
                </div>
                <div class="flex flex-col md:flex-row -mx-4">
                    <div class="md:w-1/2 px-4 mb-6 md:mb-0">
                        <div class="code-block text-gray-200 p-6 rounded-lg overflow-x-auto">
                            <pre><code>public class SynchronizedStaticMethodDemo {
    public static synchronized void staticMethod() {
        // 临界区代码
        System.out.println("静态方法锁：线程 " + 
            Thread.currentThread().getName() + " 正在执行");
    }
}</code></pre>
                        </div>
                    </div>
                    <div class="md:w-1/2 px-4">
                        <div class="bg-gray-50 p-6 rounded-lg h-full">
                            <h4 class="text-lg font-semibold mb-4 text-gray-700">作用范围与适用场景</h4>
                            <p class="mb-4"><span class="font-medium text-purple-600">锁对象：</span>锁住的是类的 Class 对象，而不是类的实例。</p>
                            <p><span class="font-medium text-purple-600">适用场景：</span>当你希望对一个类的所有实例共享的资源进行互斥访问时使用。例如，保证类级别的共享资源在多线程环境中的一致性。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 同步代码块 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="bg-purple-600 text-white w-10 h-10 rounded-full flex items-center justify-center mr-4">
                        <span class="font-bold">3</span>
                    </div>
                    <h3 class="text-2xl font-semibold text-gray-800">同步代码块（对象锁）</h3>
                </div>
                <div class="flex flex-col md:flex-row -mx-4">
                    <div class="md:w-1/2 px-4 mb-6 md:mb-0">
                        <div class="code-block text-gray-200 p-6 rounded-lg overflow-x-auto">
                            <pre><code>public class SynchronizedBlockDemo {
    private final Object lock = new Object();
    
    public void someMethod() {
        synchronized (lock) {
            // 临界区代码
            System.out.println("同步代码块锁：线程 " + 
                Thread.currentThread().getName() + " 正在执行");
        }
    }
}</code></pre>
                        </div>
                    </div>
                    <div class="md:w-1/2 px-4">
                        <div class="bg-gray-50 p-6 rounded-lg h-full">
                            <h4 class="text-lg font-semibold mb-4 text-gray-700">作用范围与适用场景</h4>
                            <p class="mb-4"><span class="font-medium text-purple-600">锁对象：</span>锁住的是同步代码块中的 <code class="bg-gray-200 px-1 rounded">lock</code> 对象。</p>
                            <p><span class="font-medium text-purple-600">适用场景：</span>当你希望只对某个代码块进行同步，而不是整个方法时使用。这种方式可以减小锁的作用范围，提高并发性能。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 类对象锁 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="bg-purple-600 text-white w-10 h-10 rounded-full flex items-center justify-center mr-4">
                        <span class="font-bold">4</span>
                    </div>
                    <h3 class="text-2xl font-semibold text-gray-800">类对象锁（类锁）</h3>
                </div>
                <div class="flex flex-col md:flex-row -mx-4">
                    <div class="md:w-1/2 px-4 mb-6 md:mb-0">
                        <div class="code-block text-gray-200 p-6 rounded-lg overflow-x-auto">
                            <pre><code>public class SynchronizedClassObjectDemo {
    public void someMethod() {
        synchronized (SynchronizedClassObjectDemo.class) {
            // 临界区代码
            System.out.println("类对象锁：线程 " + 
                Thread.currentThread().getName() + " 正在执行");
        }
    }
}</code></pre>
                        </div>
                    </div>
                    <div class="md:w-1/2 px-4">
                        <div class="bg-gray-50 p-6 rounded-lg h-full">
                            <h4 class="text-lg font-semibold mb-4 text-gray-700">作用范围与适用场景</h4>
                            <p class="mb-4"><span class="font-medium text-purple-600">锁对象：</span>锁住的是类的 <code class="bg-gray-200 px-1 rounded">Class</code> 对象。</p>
                            <p><span class="font-medium text-purple-600">适用场景：</span>当你希望对某个类的所有实例共享的静态资源进行同步时使用。例如，类级别的静态变量的访问。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- this锁 -->
            <div class="mb-16">
                <div class="flex items-center mb-6">
                    <div class="bg-purple-600 text-white w-10 h-10 rounded-full flex items-center justify-center mr-4">
                        <span class="font-bold">5</span>
                    </div>
                    <h3 class="text-2xl font-semibold text-gray-800">this锁（对象锁）</h3>
                </div>
                <div class="flex flex-col md:flex-row -mx-4">
                    <div class="md:w-1/2 px-4 mb-6 md:mb-0">
                        <div class="code-block text-gray-200 p-6 rounded-lg overflow-x-auto">
                            <pre><code>public class SynchronizedThisDemo {
    public void someMethod() {
        synchronized (this) {
            // 临界区代码
            System.out.println("this 锁：线程 " + 
                Thread.currentThread().getName() + " 正在执行");
        }
    }
}</code></pre>
                        </div>
                    </div>
                    <div class="md:w-1/2 px-4">
                        <div class="bg-gray-50 p-6 rounded-lg h-full">
                            <h4 class="text-lg font-semibold mb-4 text-gray-700">作用范围与适用场景</h4>
                            <p class="mb-4"><span class="font-medium text-purple-600">锁对象：</span>锁住的是当前对象的 <code class="bg-gray-200 px-1 rounded">this</code> 引用。</p>
                            <p><span class="font-medium text-purple-600">适用场景：</span>当你希望锁定当前对象实例，以保护实例变量或方法时使用。这种方式是 <code class="bg-gray-200 px-1 rounded">synchronized</code> 关键字最常用的场景之一。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section id="summary" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-center text-gray-800">关键要点总结</h2>
            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                <div class="summary-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-3">
                            <i class="fas fa-object-group text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">实例方法锁</h3>
                    </div>
                    <p class="text-gray-600">锁住对象实例，保证同一个对象的同步。</p>
                </div>
                <div class="summary-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-3">
                            <i class="fas fa-cube text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">静态方法锁</h3>
                    </div>
                    <p class="text-gray-600">锁住类的 Class 对象，保证类级别的同步。</p>
                </div>
                <div class="summary-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-3">
                            <i class="fas fa-code text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">同步代码块</h3>
                    </div>
                    <p class="text-gray-600">锁住指定对象，范围可以控制得更加精确。</p>
                </div>
                <div class="summary-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-3">
                            <i class="fas fa-project-diagram text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">类对象锁</h3>
                    </div>
                    <p class="text-gray-600">锁住类的 Class 对象，类似于静态方法锁。</p>
                </div>
                <div class="summary-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-3">
                            <i class="fas fa-key text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">this锁</h3>
                    </div>
                    <p class="text-gray-600">锁住当前对象实例，适用于保护实例方法或变量。</p>
                </div>
                <div class="summary-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-3">
                            <i class="fas fa-lightbulb text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">最佳实践</h3>
                    </div>
                    <p class="text-gray-600">尽量减小同步范围，使用最精确的锁对象，避免死锁。</p>
                </div>
            </div>
        </section>

        <!-- Comparison Table -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">同步方式对比</h2>
            <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">同步方式</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">锁对象</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">作用范围</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium">实例方法锁</td>
                            <td class="px-6 py-4 whitespace-nowrap">对象实例</td>
                            <td class="px-6 py-4 whitespace-nowrap">同一对象实例的方法调用</td>
                            <td class="px-6 py-4">保护实例变量或方法</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium">静态方法锁</td>
                            <td class="px-6 py-4 whitespace-nowrap">类的Class对象</td>
                            <td class="px-6 py-4 whitespace-nowrap">所有类实例的静态方法</td>
                            <td class="px-6 py-4">保护静态变量或方法</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium">同步代码块</td>
                            <td class="px-6 py-4 whitespace-nowrap">指定对象</td>
                            <td class="px-6 py-4 whitespace-nowrap">代码块范围内</td>
                            <td class="px-6 py-4">更细粒度的同步控制</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium">类对象锁</td>
                            <td class="px-6 py-4 whitespace-nowrap">类的Class对象</td>
                            <td class="px-6 py-4 whitespace-nowrap">所有类实例</td>
                            <td class="px-6 py-4">保护类级别的共享资源</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium">this锁</td>
                            <td class="px-6 py-4 whitespace-nowrap">当前对象实例</td>
                            <td class="px-6 py-4 whitespace-nowrap">同一对象实例的代码块</td>
                            <td class="px-6 py-4">保护当前实例的代码块</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold text-white">技术小馆</h3>
                    <p class="text-sm mt-1">探索技术之美，分享编程之道</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 inline-flex items-center">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>